package com.gamehall.util;

import java.io.FileInputStream;
import java.io.Reader;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionUtil {
	private static SqlSessionFactory sqlSessionFactory;

	static {
		try {
			Properties pro = new Properties();
			FileInputStream in = new FileInputStream(PropertiesUtil.getDBProperties());
			pro.load(in);
			in.close();
			Reader reader = Resources.getResourceAsReader("Configuration.xml");
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, pro);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static SqlSessionFactory getSessionFactory() {
		return sqlSessionFactory;
	}
	public static SqlSession getSession() {
		return sqlSessionFactory.openSession();
	}
	
	public static <T> T  getMapper(Class<T> c){
		return getSession().getMapper(c);
	}
}
